{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "62bce6fd-04ec-4758-af92-74a564928178",
   "metadata": {},
   "source": [
    "## Add Go build flag `-cover` to generate coverage information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5264a66e-1966-4d3e-a280-bf0b8d304f91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "%goflags=[\"-cover\"]\n"
     ]
    }
   ],
   "source": [
    "%goflags -cover"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b9c6dd42-649a-4c2a-8d58-83ed0479b560",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "%goflags=[\"-cover\"]\n"
     ]
    }
   ],
   "source": [
    "%goflags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4a69b5c4-31fe-415e-a35f-90bf5be6cce7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "%goflags=[]\n"
     ]
    }
   ],
   "source": [
    "// Clear %goflags\n",
    "%goflags \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "035d1bbc-44d3-450d-ad46-f42d464e7f6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "%goflags=[\"-cover\"]\n"
     ]
    }
   ],
   "source": [
    "%goflags -cover"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49a8323d-226f-4c55-8966-7a0bac07f8f5",
   "metadata": {},
   "source": [
    "## Create temporary `GOCOVERDIR` directory to hold coverage information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "54e882ef-9fc8-4534-aa23-9293421e5326",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Set: GOCOVERDIR=\"/tmp/gonb_examples_test_goflags_cover\"\n"
     ]
    }
   ],
   "source": [
    "%env GOCOVERDIR /tmp/gonb_examples_test_goflags_cover\n",
    "!mkdir -p $GOCOVERDIR ; rm -f ${GOCOVERDIR}/*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "da334c4a-3baa-4511-ac09-3bcc087bc071",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n"
     ]
    }
   ],
   "source": [
    "func A() {\n",
    "    fmt.Println(\"A\")\n",
    "}\n",
    "\n",
    "func B() {\n",
    "    fmt.Println(\"B\")\n",
    "}\n",
    "\n",
    "%%\n",
    "A()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e95c25c2-eec7-4ecf-8631-e7f5e2ad24c7",
   "metadata": {},
   "source": [
    "We expect that function A has full coverage, and function B has 0% coverage, since it was not called."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "42a633ce-5a09-4df8-ac24-7843b46c47a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gonb_49622dd8/main.go:8:\tA\t\t100.0%\n",
      "gonb_49622dd8/main.go:12:\tB\t\t0.0%\n",
      "gonb_49622dd8/main.go:17:\tmain\t\t100.0%\n",
      "total\t\t\t\t(statements)\t75.0%\n"
     ]
    }
   ],
   "source": [
    "!go tool covdata func -i \"${GOCOVERDIR}\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ded66e60-2ac0-468a-af1e-b985d10690f4",
   "metadata": {},
   "source": [
    "## Manually run `go build` to inspect its output\n",
    "\n",
    "This can be achieved by manually running it, independent of `GoNB`, simply using it's ability of running shell commands in the temporary directory.\n",
    "\n",
    "First we clear the cache of `GoNB`, using `%reset` and then we create a small program to analyse if variables escape to heap (Go's garbage collection flag `-gcflag=-m`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b0616c69-2eb0-4038-8963-c76b4f60c73a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "* State reset: all memorized declarations discarded.\n"
     ]
    }
   ],
   "source": [
    "%reset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d3b2bb37-f451-41ec-86f9-985fda4de4f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "type Point struct{ X, Y float64}\n",
    "\n",
    "func (p *Point) ManhattanLen() float64 {\n",
    "    return p.X + p.Y\n",
    "}\n",
    "\n",
    "%%\n",
    "p := Point{3,4}\n",
    "fmt.Println(p.ManhattanLen())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0814bcf2-0cdb-4745-9f27-c4bcf73ebb88",
   "metadata": {},
   "source": [
    "Display optimization strategy of the code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "49082319-19a5-4715-b6de-9373780ba727",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "# gonb_49622dd8\n",
      "./main.go:10:6: can inline (*Point).ManhattanLen\n",
      "./main.go:16:12: inlining call to flag.Parse\n",
      "./main.go:18:27: inlining call to (*Point).ManhattanLen\n",
      "./main.go:18:12: inlining call to fmt.Println\n",
      "./main.go:10:7: p does not escape\n",
      "./main.go:18:12: ... argument does not escape\n",
      "./main.go:18:27: ~r0 escapes to heap\n"
     ]
    }
   ],
   "source": [
    "!*go build -gcflags=-m"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Go (gonb)",
   "language": "go",
   "name": "gonb"
  },
  "language_info": {
   "codemirror_mode": "",
   "file_extension": ".go",
   "mimetype": "text/x-go",
   "name": "go",
   "nbconvert_exporter": "",
   "pygments_lexer": "",
   "version": "go1.25.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
